/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.edu.vttu.phong;

import java.awt.Component;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.table.TableModel;
import vn.edu.vttu.model.KhachHang;
import vn.edu.vttu.model.NhanVien;
import vn.edu.vttu.readxml.ReadXML;
import vn.edu.vttu.taikhoan.thongtindangnhap;

/**
 *
 * @author nhphuoc
 */
public class Phong_NhanPhong extends javax.swing.JFrame {

    class ItemRenderer extends BasicComboBoxRenderer {

        public Component getListCellRendererComponent(
                JList list, Object value, int index,
                boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index,
                    isSelected, cellHasFocus);

            if (value != null) {
                KhachHang item = (KhachHang) value;
                // đây là thông tin ta sẽ hiển thị , đối bảng khác sẽ khác cột chúng ta sẽ đổi lại tên tương ứng
                setText(item.getTenKH().toUpperCase());
            }

            if (index == -1) {
                KhachHang item = (KhachHang) value;
                setText("" + item.getTenKH());
            }

            return this;
        }
    }

    /**
     * Creates new form Phong_NhanPhong
     */
    public Phong_NhanPhong() {
        initComponents();
        fillkhCombo();
        lbTenPhong.setText(maphong.getTenphong());
    }

    private void fillkhCombo() {
        Vector<KhachHang> model = new Vector<KhachHang>();  // khai báo vector đón dữ liệu từ server rả về
        try {
            ReadXML read = new ReadXML();
            Registry registry = LocateRegistry.getRegistry(read.getInfo("ipServer"), Integer.parseInt(read.getInfo("portserver")));
            vn.edu.vttu.RMI_Interface common = (vn.edu.vttu.RMI_Interface) registry.lookup("ServerKhachSan");
            model = common.selectKhachHang();

        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(rootPane, "Kết nối Server thất bại ");
        }

        DefaultComboBoxModel defaultComboBoxModel1 = new javax.swing.DefaultComboBoxModel(model);//model là  Vector<ParentRole> model = new Vector<ParentRole>();
        cobKhachHang.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
        cobKhachHang.setModel(defaultComboBoxModel1);
        cobKhachHang.setRenderer(new Phong_NhanPhong.ItemRenderer());  // chúng ta gỏ giống vậy
    }

    private String now() {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        Date now = new Date(System.currentTimeMillis());
        return sdf.format(now);
    }

    private boolean compareDate(Date ngay1, Date ngay2) {
        int x = (ngay1.getYear()+1900) * 12 * 31 + (ngay1.getMonth()+1) * 31 + ngay1.getDate();
        int y = (ngay2.getYear()+1900) * 12 * 31 + (ngay2.getMonth()+1) * 31 + ngay2.getDate();
        if (x <= y) {
            return false;
        }
        return true;
    }

    /**
     * Ham kiem tra xem, neu la so tra ve true, nguoc lai la false
     *
     * @param num
     * @return
     */
    private boolean testNumber(String num) {
        boolean test = false;
        if (num.equals("") && num.length() == 0) {
            test = true;
        } else {
            for (int i = 0; i < num.length(); i++) {
                if (Character.isLetter(num.charAt(i))) {
                    test = false;
                    break;
                }
                if (i + 1 == num.length()) {
                    test = true;
                }
            }
        }
        return test;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cobKhachHang = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        dtNgayDat = new org.jdesktop.swingx.JXDatePicker();
        jLabel6 = new javax.swing.JLabel();
        dtNgayNhan = new org.jdesktop.swingx.JXDatePicker();
        jLabel7 = new javax.swing.JLabel();
        dtNgayTra = new org.jdesktop.swingx.JXDatePicker();
        btnNhanPhong = new javax.swing.JButton();
        lbTenPhong = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("NHẬN PHÒNG");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("NHẬN PHÒNG");

        jLabel2.setText("Phòng: ");

        jLabel4.setText("Khách Hàng");

        jLabel5.setText("Ngày Đặt");

        jLabel6.setText("Ngày Nhận");

        jLabel7.setText("Ngày Trả");

        btnNhanPhong.setText("Nhận");
        btnNhanPhong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNhanPhongActionPerformed(evt);
            }
        });

        lbTenPhong.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lbTenPhong.setForeground(new java.awt.Color(0, 0, 255));
        lbTenPhong.setText("tenphong");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(dtNgayDat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(19, 19, 19)
                        .addComponent(dtNgayTra, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(dtNgayNhan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel2))
                        .addGap(7, 7, 7)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lbTenPhong)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(cobKhachHang, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(108, 108, 108)
                .addComponent(btnNhanPhong, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(110, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(lbTenPhong, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(cobKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dtNgayDat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dtNgayNhan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dtNgayTra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(37, 37, 37)
                .addComponent(btnNhanPhong)
                .addContainerGap(32, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNhanPhongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNhanPhongActionPerformed
        try {
            int idphong = maphong.getIdphong();//Integer.parseInt(lbIDphong.getText());
            KhachHang nv = (KhachHang) cobKhachHang.getSelectedItem();
            int idkh = nv.getIdKH();
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date dt = dtNgayDat.getDate();
            String ngaydat = df.format(dt);
            Date dt1 = dtNgayNhan.getDate();
            String ngaynhan = df.format(dt1);
            Date dt2 = dtNgayTra.getDate();
            String ngaytra = df.format(dt2);
            if (compareDate(dt, dt1) == true) {
                JOptionPane.showMessageDialog(rootPane, "Ngày đặt phải nhỏ hơn ngày nhận");
                dtNgayDat.requestFocus();
            } else if (compareDate(dt1, dt2) == true) {
                JOptionPane.showMessageDialog(rootPane, "Ngày trả phải lớn hơn ngày nhận");
                dtNgayTra.requestFocus();           
            } else {
                int soluong = 1;                
                ReadXML read = new ReadXML();
                int idNV = thongtindangnhap.getIdnv();
                Registry registry = LocateRegistry.getRegistry(read.getInfo("ipServer"), Integer.parseInt(read.getInfo("portserver")));
                vn.edu.vttu.RMI_Interface common = (vn.edu.vttu.RMI_Interface) registry.lookup("ServerKhachSan");
                boolean x = common.nhanphong(idphong, idkh, ngaydat, ngaynhan, ngaytra, idNV);
                if (x) {
                    JOptionPane.showMessageDialog(rootPane, "Đã nhận phòng thành công");
                } else {
                    JOptionPane.showMessageDialog(rootPane, "Nhận phòng không thành công");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_btnNhanPhongActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;

                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Phong_NhanPhong.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Phong_NhanPhong.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Phong_NhanPhong.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Phong_NhanPhong.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Phong_NhanPhong().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNhanPhong;
    private javax.swing.JComboBox cobKhachHang;
    private org.jdesktop.swingx.JXDatePicker dtNgayDat;
    private org.jdesktop.swingx.JXDatePicker dtNgayNhan;
    private org.jdesktop.swingx.JXDatePicker dtNgayTra;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel lbTenPhong;
    // End of variables declaration//GEN-END:variables
}
